package com.googlecode.controlecheque.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DataUtil {

	private static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy";
	private static SimpleDateFormat dateFormat = new SimpleDateFormat();

	public static Long obterDataEmMillis(String strData) throws ParseException {
		return parse(strData, null).getTime();
	}

	public static Date parse(String strData, String patternParametro) throws ParseException {
		String pattern;

		if (patternParametro == null) {
			pattern = DEFAULT_DATE_FORMAT;
		} else {
			pattern = patternParametro;
		}

		dateFormat.applyPattern(pattern);
		return dateFormat.parse(strData);
	}

	public static String formatar(Date data, String patternParametro) {
		String pattern;

		if (patternParametro == null) {
			pattern = DEFAULT_DATE_FORMAT;
		} else {
			pattern = patternParametro;
		}

		dateFormat.applyPattern(pattern);
		return dateFormat.format(data);
	}

	public static Date dataDeMillis(Long millis) {
		return new Date(millis);
	}

	public static Calendar getCalendarSemHorario(final Date data) {
		if (data == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(data);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal;
	}

	public static Date getDateSemHorario(final Date data) {
		if (data == null) {
			return null;
		}
		return getCalendarSemHorario(data).getTime();
	}

}
